#include "TollgateScene.h"
#include "./utils/CSLoaderUtils.h"
#include "ui/UIButton.h"
#include "ui/UIHelper.h"
#include "../manager/MonsterManager.h"

enum class NodeZIndex :int {
	BG1 = 1,
	BG2,
	LAYERCSB,
	TITLE,
	PLAYER,
	MONSTERMGR,
	LOADINGBAR,
};

Scene* TollgateScene::createScene() {
	auto scene = Scene::create();
	// 'layer' is an autorelease object
	auto layer = TollgateScene::create();

	// add layer as a child to scene
	scene->addChild(layer);
	return scene;
}

bool TollgateScene::init() {
	if (!Scene::init()) {
		return false;
	}

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Sprite* titleSprite = Sprite::create("part1/title.png");
	titleSprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height-titleSprite->getContentSize().height/2));

	this->addChild(titleSprite, (int)( NodeZIndex::TITLE));


	m_player = Player::create();
	m_player->bindSprite(Sprite::create("part1/sprite.png"));
	m_player->setPosition(Vec2(200, 80));

	this->addChild(m_player, (int)NodeZIndex::PLAYER);

	
	initBG();

	initUINode();

	addMonsterManager();

	this->scheduleUpdate();

	return true;
}

void TollgateScene::initBG() {
	Size visibleSize = Director::getInstance()->getVisibleSize();

	m_bgSprite1 = Sprite::create("part1/tollgateBG.jpg");
	m_bgSprite2 = Sprite::create("part1/tollgateBG.jpg");

	m_bgSprite1->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
	m_bgSprite2->setPosition(Vec2(visibleSize.width + visibleSize.width/2, visibleSize.height/2));

	m_bgSprite2->setFlippedX(true);

	addChild(m_bgSprite1, (int)NodeZIndex::BG1);
	addChild(m_bgSprite2, (int)NodeZIndex::BG2);

}

void TollgateScene::initUINode() {
	std::string strPath = "cocosstudio/Layer.csb";

	auto node = CSLoaderUtils::createNodeWithCSB(strPath);
	addChild(node, (int)NodeZIndex::LAYERCSB);

	auto nodeJump = node->getChildByName("JumpBtn");
	auto btnComp = dynamic_cast<ui::Button*>(nodeJump);
	// error: ui::Button* jumpBtn = (ui::Button*)node->getChildByName("JumpBtn")->getComponent("Button");
	// jumpBtn->initWithFile("cocosstudio/button.png");
	std::string buttonPath = "cocosstudio/button.png";
	btnComp->loadTextures(buttonPath, buttonPath, buttonPath);
	// ui::Widget::ccWidgetTouchCallback callBack = jumpEvent;

	auto loadingBar = node->getChildByName("LoadingBar");
	auto score = node->getChildByName("score");

	m_hpBar = dynamic_cast<ui::LoadingBar*>(loadingBar);
	m_scoreLab = dynamic_cast<ui::Text*>(score);

	//m_hpBar = ui::LoadingBar::create("cocosstudio/Default/LoadingBarFile.png");
	//addChild(m_hpBar, (int)NodeZIndex::LOADINGBAR);
	//m_hpBar->setPosition(Point(690, 24));
	//m_hpBar->setDirection(ui::LoadingBar::Direction::LEFT);
	//m_hpBar->setColor(Color3B(255, 0, 0));


	m_hpBar->loadTexture("cocosstudio/Default/LoadingBarFile.png");


	btnComp->addTouchEventListener([&](Ref*, ui::Widget::TouchEventType type) {
		switch (type){
		case cocos2d::ui::Widget::TouchEventType::ENDED:
			m_player->jump();
			break;
		}
	});
}

void TollgateScene::jumpEvent(Ref*, ui::Widget::TouchEventType type) {
	switch (type)
	{
	/*case cocos2d::ui::Widget::TouchEventType::BEGAN:
		break;
	case cocos2d::ui::Widget::TouchEventType::MOVED:
		break;
		*/
	case cocos2d::ui::Widget::TouchEventType::ENDED:
		m_player->jump();
		break;
	/*case cocos2d::ui::Widget::TouchEventType::CANCELED:
		break;
	default:
		break;
		*/
	}
}

void TollgateScene::addMonsterManager() {
	MonsterManager* monsterMgr = MonsterManager::create();
	addChild(monsterMgr, (int)NodeZIndex::MONSTERMGR);

	monsterMgr->bindPlayer(m_player);
	monsterMgr->enableMonsters();
}

void TollgateScene::update(float delta) {
	// cocos2d::log("update");
	Size visibleSize = Director::getInstance()->getVisibleSize();

	int posX1 = m_bgSprite1->getPositionX();
	int posX2 = m_bgSprite2->getPositionX();

	int iSpeed = 1;

	posX1 -= iSpeed;
	posX2 -= iSpeed;

	float bgWidth = m_bgSprite1->getContentSize().width;
	float bgHalfWidth = bgWidth * 0.5;
	if (posX1 + bgHalfWidth <= 0) {
		posX1 = (posX2 + bgWidth);
	}

	if (posX2 + bgHalfWidth <= 0) {
		posX2 = (posX1 + bgWidth);
	}

	m_bgSprite1->setPositionX(posX1);
	m_bgSprite2->setPositionX(posX2);


	m_iScore += 1;
	m_scoreLab->setString(Value(m_iScore).asString());

	float percent = m_player->getHP() / 1000.0f * 100;
	m_hpBar->setPercent(percent);
}